package com.jiinfo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.ActionKey;
import com.jfinal.plugin.activerecord.Record;
import com.jiinfo.base.BaseController;
import com.jiinfo.model.Dic;
import com.jiinfo.vo.BaseVo;

public class DicController extends BaseController{

	@ActionKey("/manage/dic/")
	public void manage_dic(){
		
		String childName="child";
		List<Record> dicTree=Dic.dao.getTreedics(childName);
		List<Record> list=new ArrayList<>();
		for (Record record : dicTree) {
			getListForTree(childName,record,list);
		}
		
		setAttr("dicList", list);
		setAttr("pageTitle", getPageTitle("字典管理", "",
				new String[]{"<i class='icon-home'></i>首页","字典管理"},
				new String[]{"/manage","/manage/dic/"}));
		renderJsp("/manage/dic/dic-list.jsp");
		
	}
	
	
	@ActionKey("/manage/dic/add/")
	public void manage_add(){
		Record user=getCurrUser();
		
		Dic vo=getModel(Dic.class,"vo");
		Dic.dao.add(vo,user);
		
		redirect("/manage/dic/");
		
	}
	
	@ActionKey("/manage/dic/edit/")
	public void manage_edit(){
		Record user=getCurrUser();
		
		Dic vo=getModel(Dic.class,"vo");
		Dic.dao.edit(vo,user);
		
		redirect("/manage/dic/");
		
	}
	
	@ActionKey("/manage/dic/remove/")
	public void manage_remove(){
		
		String id=getPara("id");
		Dic.dao.remove(id);
		redirect("/manage/dic/");
		
	}
	
	@ActionKey("/manage/dic/viewJson/")
	public void manage_viewJson(){
		
		String id=getPara("id");
		
		Map<String,Object> map=new HashMap<>();
		Dic dic=Dic.dao.findById(id);
		Dic dicParent=Dic.dao.findById(dic.getStr("parentId"));
		map.put("dic", dic);
		map.put("dicParent", dicParent);
		
		BaseVo baseVo=new BaseVo();
		baseVo.setFlag(true);
		baseVo.setData(map);
		
		renderJson(baseVo);
		
	}
}
